﻿using AutoMapper;
using Huirui.Cavan.Domain.MainBoundedContext.StockModule;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.StockModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Presentation.ViewModel.PurchaseModule;
using Huirui.Cavan.Core.Extension;
using Huirui.Cavan.Domain.MainBoundedContext.SalesModule;
using Huirui.Cavan.Presentation.ViewModel.SalesModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Application.MainBoundedContext.Setting;

namespace Huirui.Cavan.Presentation.Controller.HomeModule
{
    public class AutoMapperStartupTask : IStartupTask
    {
        public void Execute()
        {
            Mapper.CreateMap<Stock, StockJson>()
                .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                .ForMember(d => d.UnitTransfer, opt => opt.MapFrom(m => m.UnitTransfer.FromNumber.ToString("N" + HuiruiProject.UserSettings.Precision.ToString()) + m.UnitTransfer.FromUnit.Name + "/" + m.UnitTransfer.ToNumber.ToString("N" + HuiruiProject.UserSettings.Precision.ToString()) + m.UnitTransfer.ToUnit.Name))
                .ForMember(d => d.Type, opt => opt.MapFrom(m => m.Type.Name))
                .ForMember(d => d.AssistantNumber, opt => opt.MapFrom(m => m.UnitTransfer.FromNumber == 0 ? 0 : (m.LeftNumber * m.UnitTransfer.ToNumber) / m.UnitTransfer.FromNumber))
                .ForMember(d => d.Nature, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(StockIdentifier.StockNature), m.Nature.ToString())))
                .ForMember(d => d.FromNumber, opt => opt.MapFrom(m => m.UnitTransfer.IsNotNull() ? m.UnitTransfer.FromNumber : 1))
                .ForMember(d => d.ToNumber, opt => opt.MapFrom(m => m.UnitTransfer.IsNotNull() ? m.UnitTransfer.ToNumber : 1))
                .ForMember(d => d.StandardPurchasePrice, opt => opt.MapFrom(m => m.StandardPurchasePrice.ToString("N" + HuiruiProject.UserSettings.Precision.ToString())))
                .ForMember(d => d.StandardSalesPrice, opt => opt.MapFrom(m => m.StandardSalesPrice.ToString("N" + HuiruiProject.UserSettings.Precision.ToString())))
                .ForMember(d => d.StockUnit, opt => opt.MapFrom(m => m.StockUnit.Name));
            Mapper.CreateMap<StockInOutDetailJson, StockInOutDetail>()
                .ForMember(d => d.CostType, opt => opt.UseValue(StockIdentifier.CostType.StandardPrice));
            Mapper.CreateMap<StockInOutDetail, StockInOutDetailJson>()
                .ForMember(d => d.StockID, opt => opt.MapFrom(m => m.Stock.Id))
                .ForMember(d => d.StockName, opt => opt.MapFrom(m => m.Stock.Name))
                .ForMember(d => d.WareHouse, opt => opt.MapFrom(m => m.WareHouse.Address))
                .ForMember(d => d.FromAccount, opt => opt.MapFrom(m => m.FromAccount.Name))
                .ForMember(d => d.Specification, opt => opt.MapFrom(m => m.Stock.Specification));
            Mapper.CreateMap<UnPay, UnPayJson>()
                .ForMember(d => d.CertificateDate, opt => opt.UseValue(DateTime.Now.ToShortDateString()))
                .ForMember(d => d.UnPayType, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(PurchaseIdentifier.UnPayType), m.UnPayType.GetHashCode().ToString())))
                .ForMember(d => d.CreateDate, opt => opt.MapFrom(m => m.CreateDate.ToShortDateString()))
                .ForMember(d => d.SupplierCode, opt => opt.MapFrom(m => m.Supplier.Id))
                .ForMember(d => d.SupplierName, opt => opt.MapFrom(m => m.Supplier.Name))
                .ForMember(d => d.PaidAmount, opt => opt.MapFrom(m => m.Cost - m.LeftAmount));
            Mapper.CreateMap<Pay, PayJson>()
                 .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                 .ForMember(d => d.PayDate, opt => opt.MapFrom(m => m.PayDate.ToString("yyyy-MM-dd")))
                 .ForMember(d => d.SupplierName, opt => opt.MapFrom(m => m.Supplier.Name))
                 .ForMember(d => d.EmployeeName, opt => opt.MapFrom(m => m.PayEmployee.Name))
                 .ForMember(d => d.Producer, opt => opt.MapFrom(m => m.Producer.Employee.Name))
                 .ForMember(d => d.Amount, opt => opt.MapFrom(m => m.Amount))
                 .ForMember(d => d.AccountCode, opt => opt.MapFrom(m => m.FromAccount.Id))
                 .ForMember(d => d.AccountName, opt => opt.MapFrom(m => m.FromAccount.Name));
            Mapper.CreateMap<PurchaseOrder, PurchaseOrderJson>()
                .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                .ForMember(d => d.ProjectCode, opt => opt.MapFrom(m => m.Project.IsNotNull() ? m.Project.Id : string.Empty))
                .ForMember(d => d.PurchaseEmployeeCode, opt => opt.MapFrom(m => m.PurchaseEmployee.IsNotNull() ? m.PurchaseEmployee.Id : string.Empty))
                .ForMember(d => d.SupplierCode, opt => opt.MapFrom(m => m.Supplier.IsNotNull() ? m.Supplier.Id : string.Empty))
                .ForMember(d => d.WareHouseCode, opt => opt.MapFrom(m => m.WareHouse.IsNotNull() ? m.WareHouse.Id : string.Empty))
                .ForMember(d => d.SupplierName, opt => opt.MapFrom(m => m.Supplier.IsNotNull() ? m.Supplier.Name : string.Empty))
                .ForMember(d => d.WareHouseAddress, opt => opt.MapFrom(m => m.WareHouse.IsNotNull() ? m.WareHouse.Address : string.Empty));
            Mapper.CreateMap<SalesOrder, SalesOrderJson>()
                .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                .ForMember(d => d.Id, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                .ForMember(d => d.SaleEmployeeCode, opt => opt.MapFrom(m => m.SaleEmployee.IsNotNull() ? m.SaleEmployee.Id : string.Empty))
                .ForMember(d => d.CustomerCode, opt => opt.MapFrom(m => m.Customer.IsNotNull() ? m.Customer.Id : string.Empty))
                .ForMember(d => d.WareHouseCode, opt => opt.MapFrom(m => m.WareHouse.IsNotNull() ? m.WareHouse.Id : string.Empty));
            Mapper.CreateMap<Purchase, PurchaseJson>()
                .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                .ForMember(d => d.Id, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                .ForMember(d => d.EmployeeCode, opt => opt.MapFrom(m => m.PurchaseEmployee.IsNotNull() ? m.PurchaseEmployee.Id : string.Empty))
                .ForMember(d => d.SupplierCode, opt => opt.MapFrom(m => m.Supplier.IsNotNull() ? m.Supplier.Id : string.Empty))
                .ForMember(d => d.WareHouseCode, opt => opt.MapFrom(m => m.WareHouse.IsNotNull() ? m.WareHouse.Id : string.Empty));
            Mapper.CreateMap<Receivables, ReceivablesJson>()
                .ForMember(d => d.CertificateDate, opt => opt.UseValue(DateTime.Now.ToShortDateString()))
                .ForMember(d => d.ReceivablesType, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SalesIdentifier.ReceivablesType), m.ReceivablesType.GetHashCode().ToString())))
                .ForMember(d => d.CreateDate, opt => opt.MapFrom(m => m.CreateDate.ToShortDateString()))
                .ForMember(d => d.CustomerCode, opt => opt.MapFrom(m => m.Customer.Id))
                .ForMember(d => d.CustomerName, opt => opt.MapFrom(m => m.Customer.Name));
            Mapper.CreateMap<Supplier, SupplierJson>()
                 .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                 .ForMember(d => d.Area, opt => opt.MapFrom(s => s.Area.Name))
                 .ForMember(d => d.Type, opt => opt.MapFrom(s => s.Type.Name))
                 .ForMember(d => d.Nature, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SystemIdentifier.SupplierNature), m.Nature.GetHashCode().ToString())));
            Mapper.CreateMap<Customer, CustomerJson>()
                 .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")));
            Mapper.CreateMap<Employee, EmployeeJson>()
                .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                .ForMember(d => d.DepartmentName, opt => opt.MapFrom(m => m.Department.Name))
                .ForMember(d => d.DepartmentId, opt => opt.MapFrom(m => m.Department.Id))
                .ForMember(d => d.EmployeJob, opt => opt.MapFrom(m => m.Job.Name))
                .ForMember(d => d.EmployeJobId, opt => opt.MapFrom(m => m.Job.Id))
                .ForMember(d => d.Sex, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SystemIdentifier.Sex), m.Sex.GetHashCode().ToString())))
                .ForMember(d => d.SMSQueryEnabled, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SystemIdentifier.SMSQueryEnabledStatus), ((SystemIdentifier.SMSQueryEnabledStatus)Convert.ToInt32(m.SMSQueryEnabled)).GetHashCode().ToString())))
                .ForMember(d => d.SMSQueryEnabledId, opt => opt.MapFrom(m => Convert.ToInt32(m.SMSQueryEnabled).ToString()))
                .ForMember(d => d.SexId, opt => opt.MapFrom(m => m.Sex.GetHashCode().ToString()))
                .ForMember(d => d.Birthday, opt => opt.MapFrom(m => m.Birthday.HasValue ? m.Birthday.Value.ToString("yyyy-MM-dd") : ""));
            Mapper.CreateMap<Account, AccountJson>()
                .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                .ForMember(d => d.InitialDate, opt => opt.MapFrom(m => m.InitialDate.ToShortDateString()))
                .ForMember(d => d.Type, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SystemIdentifier.AccountType), m.Type.GetHashCode().ToString())))
                .ForMember(d => d.Status, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SystemIdentifier.AccountStatus), m.Status.GetHashCode().ToString())))
                .ForMember(d => d.StatusId, opt => opt.MapFrom(m => m.Status.GetHashCode()))
                .ForMember(d => d.TypeValue, opt => opt.MapFrom(m => m.Type.GetHashCode().ToString()));
            Mapper.CreateMap<Department, DepartmentJson>()
                .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")));
            Mapper.CreateMap<SystemConfig, SystemConfigJson>()
                .ForMember(d => d.Type, opt => opt.MapFrom(m => m.Type.GetHashCode().ToString()))
                .ForMember(d => d.DataType, opt => opt.MapFrom(m => m.DataType.GetHashCode().ToString()))
                .ForMember(d => d.StrType, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SystemIdentifier.SystemConfigType), m.Type.GetHashCode().ToString())));
            Mapper.CreateMap<Customer, CustomerJson>()
                   .ForMember(d => d.AgentType, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SystemIdentifier.AgentType), m.AgentType.GetHashCode().ToString())))
                   .ForMember(d => d.Area, opt => opt.MapFrom(s => s.Area.Name))
                   .ForMember(d => d.PayType, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SystemIdentifier.PayType), m.PayType.GetHashCode().ToString())))
                   .ForMember(d => d.InvoiceType, opt => opt.MapFrom(m => Huirui.Cavan.Presentation.Seedwork.Ext.GetEnumDescription(typeof(SystemIdentifier.InvoiceType), m.InvoiceType.GetHashCode().ToString())));
            Mapper.CreateMap<UserAccount, UserAccountJson>()
                .ForMember(d => d.Employee, opt => opt.MapFrom(m => m.Employee.Name))
                .ForMember(d => d.Status, opt => opt.MapFrom(m => m.IsLocked == true ? "已停用" : "已启用"))
                .ForMember(d => d.IsLocked, opt => opt.MapFrom(m => m.IsLocked == true ? "1" : "0"))
                .ForMember(d => d.Password, opt => opt.UseValue("******"));
            Mapper.CreateMap<StockInitialBalance, StockInitialBalanceJson>()
                .ForMember(d => d.WareHouseAddress, opt => opt.MapFrom(m => m.WareHouse.Address))
                .ForMember(d => d.StockName, opt => opt.MapFrom(m => m.Stock.Name))
                .ForMember(d => d.Amount, opt => opt.MapFrom(m => m.Number * m.UnitCost))
                .ForMember(d => d.InitialDate, opt => opt.MapFrom(m => m.InitialDate.ToString("yyyy-MM-dd")));
            Mapper.CreateMap<SupplierType, SupplierTypeJson>()
                .ForMember(d => d.Id, opt => opt.MapFrom(m => m.Id.ToString()));
            Mapper.CreateMap<StockType, StockTypeJson>()
                 .ForMember(d => d.Id, opt => opt.MapFrom(m => m.Id.ToString()))
                 .ForMember(d => d.ModifyDate, opt => opt.MapFrom(m => m.ModifyDate.ToString("yyyy-MM-dd hh:mm:ss")))
                 .ForMember(d => d.CreateDate, opt => opt.MapFrom(m => m.CreateDate.ToString("yyyy-MM-dd hh:mm:ss")));
            Mapper.CreateMap<StockInOut, StockInOutJson>()
                 .ForMember(d => d.InOutDate, opt => opt.MapFrom(m => m.InOutDate.ToString("yyyy-MM-dd")))
                 .ForMember(d => d.SupplierName, opt => opt.MapFrom(m => m.Supplier.IsNotNull() ? m.Supplier.Name : string.Empty))
                 .ForMember(d => d.EmployeeName, opt => opt.MapFrom(m => m.Employee.IsNotNull() ? m.Employee.Name : string.Empty))
                 .ForMember(d => d.ProjectCode, opt => opt.MapFrom(m => m.Project.IsNotNull() ? m.Project.Id : string.Empty))
                 .ForMember(d => d.EmployeeCode, opt => opt.MapFrom(m => m.Employee.IsNotNull() ? m.Employee.Id : string.Empty))
                 .ForMember(d => d.SupplierCode, opt => opt.MapFrom(m => m.Supplier.IsNotNull() ? m.Supplier.Id : string.Empty))
                 .ForMember(d => d.WareHouseCode, opt => opt.MapFrom(m => m.WareHouse.IsNotNull() ? m.WareHouse.Id : string.Empty))
                 .ForMember(d => d.Producer, opt => opt.MapFrom(m => m.Producer.Employee.Name))
                 .ForMember(d => d.Amount, opt => opt.MapFrom(m => m.StockInOutDetails.Sum(siod => siod.Number * siod.UnitCost)));
            Mapper.CreateMap<PurchaseReturn, PurchaseReturnJson>()
                 .ForMember(d => d.NoPrefixId, opt => opt.MapFrom(m => m.Id.Replace(SecurityAppService.Corporation.Id + "_", "")))
                 .ForMember(d => d.ReturnDate, opt => opt.MapFrom(m => m.ReturnDate.ToString("yyyy-MM-dd")))
                 .ForMember(d => d.SupplierName, opt => opt.MapFrom(m => m.Supplier.Name))
                 .ForMember(d => d.EmployeeName, opt => opt.MapFrom(m => m.PurchaseEmployee.Name))
                 .ForMember(d => d.Producer, opt => opt.MapFrom(m => m.Producer.Employee.Name))
                 .ForMember(d => d.Amount, opt => opt.MapFrom(m => m.Purchase.StockInOut.StockInOutDetails.Sum(siod => siod.Number * siod.UnitCost)));
        }

        public int Order
        {
            get { return 1; }
        }
    }
}
